function priceapprox=constprice4
global x r T t K % price parameters
global sigma_square_bar

x = 5; r = 0.05; T = 1; t = 0; K=4; % x has to be nonzero in order P1 != 0 
my = 1; v = 1; ay = 10;
by = v*sqrt(2*ay);


sigma_square_bar = 0.09;%sigmasquarebar(my,v);

P0 = conditionedprice;

priceapprox = P0;
end

function [P0,ptptP0,ptptptP0] = conditionedprice
global x r T t K sigma_square_bar

mu = r*(T-t)-sigma_square_bar/2*(T-t);
sigma = sqrt(sigma_square_bar*(T-t));

P0 = quadgk(@(s) max(s-K,0).*lognpdf(s/x,mu,sigma)/x,-Inf, Inf,'AbsTol',1e-12,'RelTol',1e-12);%-exp(mu+sigma^2/2)^2-exp(2*mu+sigma^2)*(exp(sigma^2)-1);
ptptP0 = 0;
ptptptP0 = 0;
% ptptP0 = quadgk(@(s) ((mu-log(s)).^2/sigma^4-1/sigma^2).*max(s-K,0).*lognpdf(s,mu,sigma),-Inf, Inf);
% ptptptP0 = quadgk(@(s) ((mu-log(s)).^3/sigma^6+3*(mu-log(s))/sigma^4).*max(s-K,0).*lognpdf(s,mu,sigma),-Inf, Inf);

P0 = exp(-r*(T-t))*P0;
% ptptP0 = exp(-r*(T-t))*ptptP0;
% ptptptP0 = exp(-r*(T-t))*ptptptP0;
end

function [v2,v3] = coeffv3
global rho ay v

phiprime = @newapproach;
average=quadgk(@(y) phiprime(y).*y, -Inf, Inf); 
% here phi' is actually phi'(y)*normpdf(y,m,v), so average actually
% = y*phi'(y)*normpdf(y,m,v)=<yphi'(y)>

v3 = v/sqrt(2*ay)*rho*average;
v2 = 2*v3;
end

function result=newapproach(y) % the verification see ../testing/testcoeff3.m
global v my sigma1sqbar
z=y-my;
%A1=@(z) (z<0).*(sqrt(pi)/4*erfc(-z)-z/2.*exp(-z.^2))+(z>=0).*(sqrt(pi)/2-z.*exp(-z.^2)/2-sqrt(pi)/4*erfc(z));
A1=@(z) sqrt(pi)/2-z.*exp(-z.^2)/2-sqrt(pi)/4*erfc(z);
result = 1/sqrt(2*pi)/v*(sqrt(2*v^2)^3*A1(z/sqrt(2)/v)-2*v^2*my*exp(-z.^2/2/v^2)+my^2*sqrt(2*pi*v^2)/2*erfc(-z/sqrt(2*v^2)))-sigma1sqbar/2*erfc(-z/sqrt(2*v^2));
result = result/v^2;
% result <==> quadgk(@(s) (sigma1(s)-sigma1sqbar).*normpdf(s,my,v), -Inf, y)      /v^2;
% which is phi'(y)*normpdf(y,m,v);

end

function y = barsigmasq
global my v sigma1sqhd
%y = quadgk(@(s) sigma1sqhd(s).*normpdf(s,my,v), -Inf, Inf);
y = quadgk(@(s) 1/sqrt(2*pi)/v*exp(2*s-(s-my).^2/2/v^2), -Inf, Inf);
%y = v^2+my^2;
end